﻿@model Amazon_Book_Search.Models.SearchPage[]

@helper generateSearchHTML(Amazon_Book_Search.Models.SearchPage p)
{
    using (Ajax.BeginForm("Search", "Home", new AjaxOptions
    {
        HttpMethod = "GET",
        InsertionMode = InsertionMode.Replace,
        UpdateTargetId = "content"
    }))
    {
        <p> Title: @Html.TextBox("q")
        <input type="submit" value="Search" /></p>
    }

    if (p.Items.Count != 0)
    {     
    <table>
        <tr>
            <th>Pilt</th>
            <th>Kirjeldus</th>
            <th>Hind</th>
        </tr>
        @foreach (var item in p.Items)
        {
            <tr>
                <td>
                @if (item.Image != null)
                {
                    <img src="@item.Image.URL" alt="pilt" />
                }
                else {<p>ei leitud</p>}
                </td>                
                <td>
                @if (item.Review != null)
                {
                    foreach (var x in item.Review)
                    {
                        <p>@item.Review.Last().Content.ToString()</p>
                    }
                }
                else {<p>ei leitud</p>}
                </td>
                <td>
                     @if (item.Price != null)
                {
                    <p>@item.Price</p>
                }
                else {<p>ei leitud</p>}
                </td>
            </tr>
        }
    </table>
    <div>
        @if (p.PageNumber > 1)
        {
            <span><a href="javascript:prev('@p.Query', @(p.PageNumber-1));">Eelmine</a></span>
@*            <span>@Ajax.ActionLink("Eelmine", "Search", new RouteValueDictionary() { 
                                                                                       {"q", p.Query}, 
                                                                                       {"p", (p.PageNumber - 1).ToString()} },
                                                        new AjaxOptions
                                                        {
                                                            UpdateTargetId = "content",
                                                            HttpMethod = "GET",
                                                            InsertionMode = InsertionMode.Replace
                                                        })</span>    *@        
        }
        @if (p.PageNumber < p.NumPages)
        {
            <span><a href="javascript:next('@p.Query', @(p.PageNumber+1));">Järgmine</a></span>
@*            <span>@Ajax.ActionLink("Järgmine", "Search", new RouteValueDictionary() { 
                                                                                       {"q", p.Query}, 
                                                                                       {"p", (p.PageNumber + 1).ToString()} },
                                                        new AjaxOptions
                                                        {
                                                            UpdateTargetId = "content",
                                                            HttpMethod = "GET",
                                                            InsertionMode = InsertionMode.Replace
                                                        })</span>    *@        
        }
    </div>
    }
   
}

<div id="content">
    @generateSearchHTML(Model[1])
</div>

<div id="content_prev" style="display:none;">
    @if (Model[0] != null)
    {
        @generateSearchHTML(Model[0]);
    }    
</div>

<div id="content_next" style="display:none;">
    @if (Model[2] != null)
    {
        @generateSearchHTML(Model[2]);
    }    
</div>

